#include "mainwindow.h"
#include "mylineedit.h"
#include <QVector>
#include <QLabel>
#include <QFrame>
#include <QDebug>
#include <QKeyEvent>
#include <QApplication>
#include <QTimerEvent>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    this->setGeometry(0, 0, 1280, 800);
    this->setMaximumSize(1280, 800);
    this->setMinimumSize(1280, 800);
    nextButton = new QPushButton();
    nextButton->setText(tr("next(&n)"));
    prevButton = new QPushButton();
    prevButton->setText(tr("prev(&p)"));
    vWidget = new QWidget();
    hWidget = new QWidget();
    vBoxLayout = new QVBoxLayout();
    hBoxLayout = new QHBoxLayout();
    stackedWidget = new QStackedWidget();

    vWidget->setLayout(vBoxLayout);
    hWidget->setLayout(hBoxLayout);
    hBoxLayout->addWidget(prevButton);
    hBoxLayout->addWidget(nextButton);
    vBoxLayout->addWidget(stackedWidget);
    vBoxLayout->addWidget(hWidget);
    setCentralWidget(vWidget);

    QVector<QWidget*> mWidget(10, nullptr);
    for (int i = 0; i < mWidget.size(); i++) {
        mWidget[i] = new QWidget();
        QLabel* label = new QLabel(QString::number(i), mWidget[i]);
        Q_UNUSED(label);
        mWidget[i]->setGeometry(0, 0, stackedWidget->width(), stackedWidget->height());
        mWidget[i]->setStyleSheet(R"(QWidget{background-color:#FFFF00})");
        stackedWidget->addWidget(mWidget[i]);
    }

    /* widget 0 */
    QFrame* line = new QFrame(mWidget[0]);
    line->setFrameShape(QFrame::HLine);
    line->setFrameShadow(QFrame::Plain);
    line->setLineWidth(5);
    line->setMidLineWidth(10);
    line->setGeometry(100, 50, 200, 20);
    line->setStyleSheet(R"(QFrame{
        background-color:rgb(17, 152, 255);
        border-radius:2px;
    })");
    QLabel* text = new QLabel(mWidget[0]);
    text->setGeometry(100, 100, 500, 50);
    text->setStyleSheet(R"(QLabel{
        background-color:#00FFFF;
        border-radius:25px;
    })");
    QFont font;
    font.setFamily("宋体");
    font.setPointSize(20);
    font.setBold(true);
    font.setItalic(false);
    text->setAlignment(Qt::AlignCenter);
    text->setFont(font);
    QString str("skdjaskldjklsajdksajdlkjaslkd");
    text->setText(text->fontMetrics().elidedText(str, Qt::ElideRight, 150));

    /* widget 1 */
    QLabel* visiableText = new QLabel(mWidget[1]);
    visiableText->setText("Visiable 测试");
    visiableText->setGeometry(100, 10, 100, 80);
    text->setFont(font);
    QPushButton* visiableButton = new QPushButton(mWidget[1]);
    visiableButton->setGeometry(100, 100, 80, 20);
    visiableButton->setText("点我显示/隐藏Label");
    visiableButton->setCheckable(true);
    connect(visiableButton, &QPushButton::clicked, [=](bool clicked){
        qDebug() << clicked << endl;
        #if 1
        visiableText->setVisible(clicked);
        #else
        if (clicked) {
            visiableText->show();
        } else {
            visiableText->hide();
        }
        #endif
    });

    /* widget2 */
    MyLineEdit* lineEdit = new MyLineEdit(mWidget[2]);
    lineEdit->move(100, 100);

//    connect(prevButton, &QPushButton::clicked, this, &MainWindow::prevPager);
    connect(prevButton, SIGNAL(clicked(bool)), this, SLOT(prevPager()));
    connect(nextButton, &QPushButton::clicked, this, &MainWindow::nextPager);
    connect(stackedWidget, &QStackedWidget::currentChanged, this, &MainWindow::currentChanged);
}

MainWindow::~MainWindow()
{

}

void MainWindow::timerEvent(QTimerEvent *event)
{
    if (event->timerId() == timeId) {
        qDebug() << "--------" << endl;
    }
}

void MainWindow::nextPager()
{
    int count = stackedWidget->count();
    int index = stackedWidget->currentIndex();
    qDebug("count:%02d,index:%02d", count, index);
    stackedWidget->setCurrentIndex((index + 1) % count);
}

void MainWindow::prevPager()
{
    int count = stackedWidget->count();
    int index = stackedWidget->currentIndex();
    qDebug("count:%02d,index:%02d", count, index);
    stackedWidget->setCurrentIndex((index - 1) % count);
}

void MainWindow::currentChanged(int index)
{
    if (index == 4) {
        timeId = startTimer(1000);
    } else if (timeId != 0) {
        killTimer(timeId);
        timeId = 0;
    }

    QKeyEvent sendEvent(QEvent::KeyPress, Qt::Key_Up, Qt::NoModifier);
    /* 会堵塞直到事件返回-执行完不会释放 */
    qApp->sendEvent(this, &sendEvent);

    QKeyEvent *postEvent = new QKeyEvent(QEvent::KeyPress, Qt::Key_Up, Qt::NoModifier);
    /* 立刻返回 放入事件队列-执行完会自动释放myEvent */
    qApp->postEvent(this, postEvent);
}

